from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
# Create your views here.
import os
from datetime import datetime
from CRM.settings import MEDIA_ROOT
from student.models import Student, Grade, StudentDetail, Course, Enroll
from django.db.models import Q
from django.core.paginator import Paginator


def index(request):
    sts1 = Student.objects.filter(is_delete=False).order_by('-c_time')
    search = request.POST.get('search')

    total_sts = sts1.count()  # 总数据条数
    per_page = int(request.GET.get('per_page', 5))  # 每页多少条数据,默认为5
    page = int(request.GET.get('page', 1))  # 当前页码，默认为1

    p = Paginator(sts1, per_page, allow_empty_first_page=True)  # 允许第一页为空
    sts = p.get_page(page)    # 获取当前页码的数据
    total_page = p.num_pages  # 总的页面数
    start = ((page-1)*per_page)
    if search:
        if search.isdigit():
            sts = sts1.filter(Q(qq=search) | Q(phone=search))
        else:
            sts = sts1.filter(name=search)

    return render(request, 'student/index.html', context={
        'sts': sts,
        'search': search,
        'page': int(page),
        'per_page': int(per_page),
        'total_sts': total_sts,
        'total_page': int(total_page),
        'p': p,
    })


def is_delete(request, pk):
    student = Student.objects.get(pk=pk)
    student.is_delete = True
    student.save()
    return redirect(reverse('student:index'))


def detail(request, pk):
    section = '学生个人详情'
    grade = Grade.objects.all()
    course = Course.objects.all()
    try:
        student = Student.objects.get(pk=pk)
        stu_detail = StudentDetail.objects.get(student=student)
    except:
        student = None
        stu_detail = None
    return render(request, 'student/detail.html', context={
        'student': student,
        'grade': grade,
        'section': section,
        'course': course,
        'stu_detail': stu_detail,
    })


def student_edit(request, pk):
    if request.method == 'POST':
        try:
            student = Student.objects.get(pk=pk)
        except:
            student = Student()
        # 班级信息
        grade_id = request.POST.get('grade')
        try:
            grade = Grade.objects.get(pk=grade_id)
        except:
            grade = None

        # 学生信息
        student.name = request.POST.get('name')
        student.age = request.POST.get('age')
        student.sex = request.POST.get('sex')
        student.qq = request.POST.get('qq')
        student.phone = request.POST.get('phone')
        student.grade = grade
        student.save()

        # 学生详情
        try:
            stu_detail = StudentDetail.objects.get(student=student)  # 学生没有对应详情的话会报错，所以进行异常处理。
        except:
            stu_detail = StudentDetail()  # 创建实例对象，上面取不到信息的时候说明没有对应详情。
            stu_detail.student = student  # 表关联
        stu_detail.num = request.POST.get('num')
        stu_detail.college = request.POST.get('college')
        stu_detail.save()

        # 课程信息

        return redirect(reverse('student:index'))

    else:
        if int(pk) == 0:
            section = '添加学生信息'
            student = None
        else:
            section = '修改学生信息'
            student = Student.objects.get(pk=pk)
        grade = Grade.objects.all()
        course = Course.objects.all()
        try:
            stu_detail = StudentDetail.objects.get(student=student)
        except:
            stu_detail = None
        return render(request, 'student/detail.html', context={
            'student': student,
            'grade': grade,
            'section': section,
            'course': course,
            'stu_detail': stu_detail,
        })

from student.forms import StudentDetailForm,StudentForm


def student_edit_form(request, pk):
    section = '修改学生信息'
    student = Student.objects.get(pk=pk)
    stu_detail = StudentDetail.objects.get(student=student)
    # 学生和学生详情的form
    if request.method == 'GET':
        form = StudentForm(instance=student)
        detail_form = StudentDetailForm(instance=stu_detail)

    if request.method == 'POST':
        form = StudentForm(request.POST, instance=student)
        detail_form = StudentDetailForm(request.POST, instance=stu_detail)
        if form.is_valid() and detail_form.is_valid():
            form.save()
            detail_form.save()
            return redirect(reverse('student:index'))

    return render(request, 'student/detail_form.html', context={
        'section': section,
        'student': student,
        'form': form,
        'detail_form': detail_form,
    })